package com.jhg.drgclinicalpathway.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jhg.drgclinicalpathway.domain.PatientInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface PatientDao extends BaseMapper<PatientInfo> {

    @Select({
            "select top 1 ssrq from V手术信息 where JZH=#{id} and ssbm=#{code}"
    })
    public Date getOPtDate(@Param("id") String id,@Param("code") String code);

    @Select({
            "<script>",
            "exec PRO_LJ_病人路径评估_EXEC @病人标识=#{id},@路径ID=#{l}",
            "</script>"
    })
    public List<Map<String,Object>> PRO_LJ_病人路径评估_EXEC(@Param("id") String id,@Param("l") Integer l);

    @Select({
            "SELECT ID,评估ID,病人标识,住院天数,金额",
                    "FROM dbo.V病人路径评估_从表",
                    "WHERE 病人标识 =#{id}"
    })
    public List<Map<String,Object>> 病人天数信息(@Param("id") String id);


    @Select({
            "<script>",
            "EXEC dbo.PRO_LJ_病人路径评估_从表_EXEC @病人标识=#{id}," ,
                    "@路径ID=#{pid},",
                    "@类别=#{t}",
            "</script>",
    })
    public List<Map<String,Object>> LJ_病人路径评估_从表_EXEC(@Param("id") String id,@Param("pid") String pid,@Param("t") String t);
    @Select({
            "<script>",
            "SELECT FY.*,MX.项目ID FROM dbo.V临床路径项目明细 MX JOIN dbo.V费用信息 FY ON  MX.项目编码 = FY.XMBM",
            "WHERE FY.JZH = #{id} and MX.项目ID in",
            "<foreach collection='ls' open='(' close=')' item='bh' index='i' separator=','>",
            "#{bh}",
            "</foreach>",
            "</script>"
    })
    public List<Map<String,Object>> 超范围项目(@Param("id") String id,@Param("ls")List<String> ls);


    @Select({
            "<script>",
            "EXEC dbo.PRO_LJ_路径自动处理_AUTO #{id} ",
            "</script>"
    })
    public void PRO_LJ_路径自动处理_AUTO(@Param("id") String id);

    @Select({
            "select * from Vdcp_诊断信息 where JZH=#{id}"
    })
    public List<Map<String,Object>> 诊断信息(String id);

    @Select({
            "select * from V手术信息 where JZH=#{id}"
    })
    public List<Map<String,Object>> 手术信息(String id);


    @Select({
            "select 科室 from VDCP_病人信息 where 科室 is not null",
            "group by 科室"
    })
    public List<String> 科室();

    @Select({
            "select 科室,医生 from VDCP_病人信息 where 科室 is not null",
            "group by 科室,医生"
    })
    public List<Map<String,Object>> 科室医生();

    @Select({
            "select 医生 from VDCP_病人信息 where 医生 is not null",
            "group by 医生"
    })
    public List<String> 医生();


    @Select({
            "<script>",
            "SELECT * FROM (",
            "SELECT A.病人ID,A.[病人姓名],科室,住院号,病组编码,病组名称,路径名称 模板名称,总费用,床日,",
            "(SELECT STRING_AGG(FORMATMESSAGE(c.描述1,b.信息参数),CHAR(13) + CHAR(10)) FROM dbo.LJ_病人评估预警 (READPAST) B JOIN JC_预警信息目录 (READPAST) c ON b.预警ID = c.ID WHERE A.病人id = b.病人标识 AND b.预警ID IN (${wids})) 风险提示 ",
            "FROM [dbo].[VDCP_病人信息_路径指标参考] A",
            "WHERE 盈亏标志 = #{ykf} and ${t} between '${stime} 00:00:00' and '${etime} 23:59:59'",
            "<if test=\"ks!=null and ks!=''\">",
            "and 科室=#{ks}",
            "</if>",
            "<if test=\"warns!=null and warns!=''\">",
            "and exists( SELECT 1 FROM dbo.LJ_病人评估预警 b WHERE b.病人标识=A.病人ID AND b.预警ID=#{warns} )",
            "</if>",
            ") A ",
            "WHERE A.风险提示 IS NOT NULL",
            "</script>"
    })
    public Page<Map<String,Object>> 有盈余风险的病例评估ByPage(Page<Map<String,Object>> page,
                                                               @Param("t") String t,
                                                               @Param("stime") String stime,
                                                               @Param("etime") String etime,
                                                               @Param("ks") String ks,
                                                               @Param("ykf") Integer ykf,
                                                               @Param("wids") String wids,
                                                               @Param("warns") String warns
                                                               );
}
